﻿using System;
using System.Linq;
using System.Web.UI.WebControls;

namespace RaceList.Admin.Relay
{
    public partial class AwardEdit : System.Web.UI.Page
    {
        ipowdb_suibian_webDataContext db = ConnHelper.ipowdb_suibian_web();
        /// <summary>
        /// 传入的文章id
        /// </summary>
        protected int ID
        {
            get
            {
                if (Request.QueryString["ID"] != null)
                {
                    return Convert.ToInt32(Request.QueryString["ID"].ToString());
                }
                else
                {
                    return 0;
                }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Data_Bind();
            }
        }

        protected void Data_Bind()
        {
            var award = db.Activity_Awards.SingleOrDefault(s => s.Id == ID);
            if (award == null)
            {
                StringHelper.Alert(this, "此信息不存在或已被删除！", "AwardList.aspx");
                return;
            }

            var cycles = db.Activity_Cycle.Where(s => s.isDel == false).Select(s => new { s.Id, s.title });
            foreach (var item in cycles)
            {
                ddlActivity.Items.Add(new ListItem(item.title, item.Id.ToString()));
            }
            var sponsors = db.Activity_Sponsor.Where(s => s.isDel == false).Select(s => new { s.Id, s.Name });
            foreach (var item in sponsors)
            {
                ddlSponsor.Items.Add(new ListItem(item.Name, item.Id.ToString()));
            }
            ddlSponsor.Items.Insert(0, new ListItem("无赞助商", "null"));


            txtTitle.Text = award.name;
            txtCount.Text = award.sumCount.ToString();
            txtDay_Count.Text = award.dayCount.ToString();
            txtRemark.Text = award.remark;
            ddlStatus.SelectedIndex = CommonHelp.GetSelectIndex(award.status); //status
            ddlActivity.SelectedValue = award.cycleId.ToString();
            ddlSponsor.SelectedValue = GetSponsorValue(award.sponsorId);
        }

        private string GetSponsorValue(int? sponsorId)
        {
            if (sponsorId == null)
                return "null";
            else
                return sponsorId.ToString();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            Activity_Awards award = db.Activity_Awards.SingleOrDefault(ar => ar.Id == ID);

            award.cycleId = Convert.ToInt32((string) ddlActivity.SelectedItem.Value);
            award.dayCount = Convert.ToInt32((string) txtDay_Count.Text.Trim());
            award.editTime = DateTime.Now;
            award.name = txtTitle.Text.Trim();
            award.remark = txtRemark.Text.Trim();
            award.sponsorId = CommonHelp.GetSponsor(ddlSponsor.SelectedValue);
            award.status = CommonHelp.GetStatus(ddlStatus.SelectedIndex);
            award.sumCount = Convert.ToInt32((string) txtCount.Text.Trim());


            try
            {
                db.SubmitChanges();

                StringHelper.Alert(this, "奖品发布成功！", "AwardList.aspx");
            }
            catch (Exception ex)
            {
                this.Message.Text = "发布失败! 失败原因：" + ex.Message + "。详细错误信息请查看错误日志。";
            }
        }
    }
}